/**
 * Software License, Version 1.0
 * 
 * Copyright 2003 The Trustees of Indiana University.  All rights reserved.
 * 
 *
 *Redistribution and use in source and binary forms, with or without 
 *modification, are permitted provided that the following conditions are met:
 *
 *1) All redistributions of source code must retain the above copyright notice,
 * the list of authors in the original source code, this list of conditions and
 * the disclaimer listed in this license;
 *2) All redistributions in binary form must reproduce the above copyright 
 * notice, this list of conditions and the disclaimer listed in this license in
 * the documentation and/or other materials provided with the distribution;
 *3) Any documentation included with all redistributions must include the 
 * following acknowledgement:
 *
 *"This product includes software developed by the Community Grids Lab. For 
 * further information contact the Community Grids Lab at 
 * http://communitygrids.iu.edu/."
 *
 * Alternatively, this acknowledgement may appear in the software itself, and 
 * wherever such third-party acknowledgments normally appear.
 * 
 *4) The name Indiana University or Community Grids Lab or NaradaBrokering, 
 * shall not be used to endorse or promote products derived from this software 
 * without prior written permission from Indiana University.  For written 
 * permission, please contact the Advanced Research and Technology Institute 
 * ("ARTI") at 351 West 10th Street, Indianapolis, Indiana 46202.
 *5) Products derived from this software may not be called NaradaBrokering, 
 * nor may Indiana University or Community Grids Lab or NaradaBrokering appear
 * in their name, without prior written permission of ARTI.
 * 
 *
 * Indiana University provides no reassurances that the source code provided 
 * does not infringe the patent or any other intellectual property rights of 
 * any other entity.  Indiana University disclaims any liability to any 
 * recipient for claims brought by any other entity based on infringement of 
 * intellectual property rights or otherwise.  
 *
 *LICENSEE UNDERSTANDS THAT SOFTWARE IS PROVIDED "AS IS" FOR WHICH NO 
 *WARRANTIES AS TO CAPABILITIES OR ACCURACY ARE MADE. INDIANA UNIVERSITY GIVES
 *NO WARRANTIES AND MAKES NO REPRESENTATION THAT SOFTWARE IS FREE OF 
 *INFRINGEMENT OF THIRD PARTY PATENT, COPYRIGHT, OR OTHER PROPRIETARY RIGHTS. 
 *INDIANA UNIVERSITY MAKES NO WARRANTIES THAT SOFTWARE IS FREE FROM "BUGS", 
 *"VIRUSES", "TROJAN HORSES", "TRAP DOORS", "WORMS", OR OTHER HARMFUL CODE.  
 *LICENSEE ASSUMES THE ENTIRE RISK AS TO THE PERFORMANCE OF SOFTWARE AND/OR 
 *ASSOCIATED MATERIALS, AND TO THE PERFORMANCE AND VALIDITY OF INFORMATION 
 *GENERATED USING SOFTWARE.
 */
package cgl.narada.matching;

import cgl.narada.util.ByteUtilities;

/** This class is primarily used to maintain predicate counts for destinations
    during the matching process' traversal through the profile graph. This
    class is used by the stable storages to maintain reference counts
    associated with an event. These reference counts are used by the system
    to facilitate a distributed garbage collection scheme for the stable
    storages responsible for storing events for disconnected clients.

    @author Shrideep Pallickara
    $Revision$
    $Date$
*/

public class EventDestinations {
  /** The destinations assocaited with the event */
  private int destinations;

  /** For each destination, contains the number of predicates within that 
      destination that are interested in the event */
  private int[] predicatesInterested;
  
  public EventDestinations() {
    predicatesInterested = new int[32];  
  }
  
  /** This constructor provides the unmarshalling of a byte stream 
      representation of the event destinations 
      @param eventDestBytes The marshalled stream containing event destinations
  */
  public EventDestinations(byte[] eventDestBytes) {
    predicatesInterested = new int[32];
    byte[] intBytes = new byte[4];
    int offset =0;
    
    System.arraycopy(eventDestBytes, offset, intBytes, 0,4);
    destinations = ByteUtilities.getInt(intBytes);
    offset+=4;
    
    for (int i=0; i <32; i++) {
      System.arraycopy(eventDestBytes, offset, intBytes, 0,4);
      predicatesInterested[i] = ByteUtilities.getInt(intBytes);
      offset+=4;
    }
  }
  
  /** This method is responsible for updating the destinations, and the
      predicate counts associated with these destinations to account
      for the interest of predicates/destinations in a particular event.
      This is of interest to those nodes which have access to a stable storage.
      
      @param _destinations The destinations associated with an event.
      @param predicateCount The count associated with individual destinations
      for the event.
  */
  public synchronized void 
  updateWithDestinationsAndPredicateCount(int _destinations,
					  int[] predicateCount) {
    destinations |= _destinations;
    
    for (int i=0; i <32; i++) {
      predicatesInterested[i] =
	predicatesInterested[i] + predicateCount[i]; 
    }
  }
  
  /** Returns the list of destinations associated with the event */
  public int getDestinations() {
    return destinations;
  }
  
  
  /** Returns the predicate count associated with the destinations 
      @return The predicate count associated with individual destinations.
  */
  public int[] getPredicateCountForDestinations() {
    return predicatesInterested;
  }

  /** Provides us with a string representation of event destinations */
  public String toString() {
    String destString = "Destinations ->" + 
      ByteUtilities.printInt(destinations) + " Predicate Count ";
    String predicateString = "";
    for (int i=31; i >=0; i--) {
      predicateString = predicateString + 
	predicatesInterested[i] + " "; 
    } 
    return destString+predicateString;
  }
  
  
  /** This is the marshalling operation which we need, to write the byte stream
      to the stable storage. 
      @return The byte stream representation of event destinations 
  */
  public byte[] getBytes() {
    byte[] eventDestBytes = new byte[ 4 /** for the destinations */ +
				    4*32 /** for the predicate counts */];
    byte[] intBytes = new byte[4];
    intBytes = ByteUtilities.getBytes(destinations);
    int offset=0;
    
    System.arraycopy(intBytes, 0, 
		     eventDestBytes, offset, 4);
    offset+=4;
    
    for (int i=0; i<32; i++) {
      intBytes = ByteUtilities.getBytes(predicatesInterested[i]);
      System.arraycopy(intBytes, 0, 
		       eventDestBytes, offset, 4);
      offset+=4;
    }
    return eventDestBytes;
  }

  public void reducePredicateCountForDestination(int destination,
						 int predicateCount) {
    int destinationID = 0;
    for (int i =1; i < 32; i++) {
      if ( (destination &1) == 1) {
	destinationID = i;
	break;
      }
      destination >>= 1;
    }
    
    predicatesInterested[destinationID-1] -=  predicateCount;
    if (predicatesInterested[destinationID-1] == 0) {
      /** We have to reflect the destinations so that it doesn't contain a 
	  1 at destinationID. */
      destinations = destinations & 
	ByteUtilities.performNotOperation(destination); 
    } 
    
    if (predicatesInterested[destinationID-1] < 0) {
      System.out.println("EventDestns:reducePredicateCountForDestination" +
			 " predicate count associated with destination is " +
			 "less than zero");
    }
  }



  public static void main(String[] args) {
  }


}
